package com.wenhao.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author Wenhao Tong
 * @create 2021-08-20 11:29
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // FutureTask 实现了 RunnableFuture 又实现了 Runnable
        FutureTask<String> ft = new FutureTask<>(new MyThread());
        new Thread(ft).start();
        new Thread(ft).start(); // 结果会被缓存
        String s = ft.get(); // 可能需要等待
        System.out.println(s);
    }
}

class MyThread implements Callable<String> {

    @Override
    public String call() throws Exception {
        System.out.println("call...");
        return "123";
    }
}
